package com.freesky.biz.activity.service.impl;

import com.freesky.biz.activity.domain.*;
import com.freesky.biz.activity.mapper.ActivityConfigMapper;
import com.freesky.biz.activity.mapper.ProjectMapper;
import com.freesky.biz.activity.service.IActivityConfigService;
import com.freesky.biz.activity.service.IProjectService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.constant.UserConstants;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.exception.base.BaseException;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectServiceImpl implements IProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public TProject get(String id) {
        return projectMapper.get(id);
    }

    @Override
    public List<TProject> findList(TProject entity) {
        return projectMapper.findList(entity);
    }

    @Override
    public TProject selectSummaryData(TProject entity) {
        return projectMapper.selectSummaryData(entity);
    }

    @Override
    public int selectCouponCount(String projectId) {
        return projectMapper.selectCouponCount(projectId);
    }


    @Override
    public boolean checkProjectNameUnique(TProject entity) {
        String projectId = entity.getId();
        TProject info = projectMapper.selectProjectByName(entity.getName());
        if (StringUtils.isNotNull(info) && !info.getId().equalsIgnoreCase(projectId)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int insert(TProject entity) {
        return projectMapper.insert(entity);
    }

    @Override
    public int update(TProject entity) {
        return projectMapper.update(entity);
    }

    @Override
    public int deleteByIds(String[] ids) {
        return projectMapper.deleteByIds(ids);
    }

    @Override
    public List<TAssignCoupon> selectCouponList(TAssignCoupon assignCoupon) {
        return projectMapper.selectCouponList(assignCoupon);
    }

    @Override
    public int assignCoupon(TAssignCoupon entity) {
        String []couponIds = entity.getCouponId().split(",");
        for(int i=0;i<couponIds.length;i++){
            String couponId = couponIds[i];
            TProjectRelatedLog projectRelatedLog = new TProjectRelatedLog();
            projectRelatedLog.setId(SeqUtil.getSeqId());
            projectRelatedLog.setProjectId(entity.getProjectId());
            projectRelatedLog.setStockId(couponId);
            projectRelatedLog.setFlag("1");
            projectRelatedLog.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUserId()));
            TAssignCoupon assignCoupon = projectMapper.selectCouponById(couponId);
            if(assignCoupon==null || !"-1".equalsIgnoreCase(assignCoupon.getProjectId())){
                throw new BaseException("批次号已被关联");
            }

            if("1".equalsIgnoreCase(assignCoupon.getType())){
                projectMapper.updateCouponRelatedProject(couponId,entity.getProjectId());
                //projectMapper.updateProjectRelatedLog(projectRelatedLog);
            } else if("2".equalsIgnoreCase(assignCoupon.getType())){
                projectMapper.updateAlipayRelatedProject(couponId,entity.getProjectId());
                //projectMapper.updateProjectRelatedLog(projectRelatedLog);
            }
        }
        return 0;
    }

    @Override
    public int unAssignCoupon(TAssignCoupon entity) {
        String []couponIds = entity.getCouponId().split(",");
        for(int i=0;i<couponIds.length;i++){
            String couponId = couponIds[i];

            TProjectRelatedLog projectRelatedLog = new TProjectRelatedLog();
            projectRelatedLog.setId(SeqUtil.getSeqId());
            projectRelatedLog.setProjectId(entity.getProjectId());
            projectRelatedLog.setStockId(couponId);
            projectRelatedLog.setFlag("2");
            projectRelatedLog.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUserId()));
            TAssignCoupon assignCoupon = projectMapper.selectCouponById(couponId);
            if(assignCoupon==null){
                throw new BaseException("批次号不存在!");
            }
            if("1".equalsIgnoreCase(assignCoupon.getType())){
                projectMapper.updateCouponRelatedProject(couponId,"-1");
                //projectMapper.updateProjectRelatedLog(projectRelatedLog);
            } else if("2".equalsIgnoreCase(assignCoupon.getType())){
                projectMapper.updateAlipayRelatedProject(couponId,"-1");
                //projectMapper.updateProjectRelatedLog(projectRelatedLog);
            }
        }
        return 0;
    }

}
